package org.quickstart.dispatch.handlers;

import net.customware.gwt.dispatch.server.ActionHandler;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;
import org.quickstart.api.action.GetContactsAction;
import org.quickstart.model.Contact;

import java.util.ArrayList;

/**
 * EXAMPLE
 *
 * @author Yoav Aharoni
 */
public class GetContactsHandler implements ActionHandler<GetContactsAction, GetContactsAction.Result> {
	private ArrayList<Contact> mockContacts;

	public GetContactsHandler() {
		mockContacts = new ArrayList<Contact>();
		mockContacts.add(new Contact("Jhon", "00-123456789"));
		mockContacts.add(new Contact("Emma", "0-98-7654321"));
		mockContacts.add(new Contact("McHammer", "11-223-445"));
	}

	@Override
	public GetContactsAction.Result execute(GetContactsAction getContactsAction, ExecutionContext executionContext) throws ActionException {
		return new GetContactsAction.Result(mockContacts);
	}

	@Override
	public void rollback(GetContactsAction getContactsAction, GetContactsAction.Result result, ExecutionContext executionContext) throws ActionException {
	}

	@Override
	public Class<GetContactsAction> getActionType() {
		return GetContactsAction.class;
	}
}
